<template>
  <el-row :gutter="10">
    <el-col>
      <el-card class="layout-query">
        <el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
          <!--          <el-form-item prop="plateNo">-->
          <!--            <el-input v-model="state.queryForm.plateNo" placeholder="车牌号"></el-input>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item prop="vinNo">-->
          <!--            <el-input v-model="state.queryForm.vinNo" placeholder="VIN号"></el-input>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item prop="engineNo">-->
          <!--            <el-input v-model="state.queryForm.engineNo" placeholder="发动机号"></el-input>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item prop="imei">-->
          <!--            <el-input v-model="state.queryForm.imei" placeholder="绑定设备imei"></el-input>-->
          <!--          </el-form-item>-->
          <el-form-item prop="key">
            <el-input v-model="state.queryForm.key" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="Search" type="primary" @click="getWxList()">查询</el-button>
          </el-form-item>
<!--          <el-form-item>-->
<!--            <el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>-->
<!--          </el-form-item>-->
        </el-form>
      </el-card>
      <el-card>
        <el-space>
          <el-space>
            <el-button icon="Plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
          </el-space>
          <el-space>
            <el-button icon="Delete" plain type="danger" @click="deleteBatch(deleteArray)"
              >批量删除</el-button
            >
          </el-space>
        </el-space>
        <el-table
          v-loading="dataListLoading"
          :data="data"
          border
          class="layout-table"
          @selection-change="selectionChange">
          <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
          <el-table-column prop="appid" label="appId" header-align="center" align="center"></el-table-column>
          <el-table-column prop="name" label="公众号名称" header-align="center" align="center"></el-table-column>
          <!--          <el-table-column prop="type" label="类型" header-align="center" align="center">-->
          <ma-dict-column prop="type" label="是否当前" dict-type="official_account_type"></ma-dict-column>
          <!--          </el-table-column>-->
          <el-table-column prop="verified" label="是否当前" header-align="center" align="center">
            <template #default="scope">
              <span>{{ scope.row.verified ? '是' : '否' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="dataFormat" label="格式" header-align="center" align="center"></el-table-column>
          <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
            <template #default="scope">
              <el-button type="primary" link @click="addOrUpdateHandle(scope.row)"
                >修改</el-button
              >
              <el-button  type="primary" link @click="deleteBatch([scope.row.appid])"
                >删除</el-button
              >
<!--              <el-button v-auth="'biz:car:update'" type="primary" link @click="setCookie(scope.row.appid)"-->
<!--                >设置默认</el-button-->
<!--              >-->
              <el-button  type="primary" link @click="addOrAccessHandle(scope.row)"
                >接入</el-button
              >
            </template>
          </el-table-column>
        </el-table>
<!--        <el-pagination-->
<!--          :current-page="state.page"-->
<!--          :page-sizes="state.pageSizes"-->
<!--          :page-size="state.limit"-->
<!--          :total="state.total"-->
<!--          layout="total, sizes, prev, pager, next"-->
<!--          @size-change="sizeChangeHandle"-->
<!--          @current-change="currentChangeHandle">-->
<!--        </el-pagination>-->

        <!-- 弹窗, 新增 / 修改 -->
        <add-or-access ref="addOrAccessRef"></add-or-access>
        <add-or-update ref="addOrUpdateRef" @refreshdatalist="getWxList"></add-or-update>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { useCrud } from '@/hooks'
import { h, reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import AddOrUpdate from './add-or-update.vue'
import { deleteApi, getWxListApi } from '@/api/wx/account'
import AddOrAccess from '@/views/wx/account/add-or-access.vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const state: IHooksOptions = reactive({
  // dataListUrl: '/api/manage/wxAccount/list',
  deleteUrl: '/manage/wxAccount/delete',
  queryForm: {
    key: ''
  }
})
const data = ref()
const queryRef = ref()
const dataListLoading = ref(true)
const addOrUpdateRef = ref()
const addOrAccessRef = ref()
const deleteArray = ref([])
const addOrUpdateHandle = (data?: number) => {
  // console.log(id)
  addOrUpdateRef.value.init(data)
}
const addOrAccessHandle = (data?: number) => {
  addOrAccessRef.value.init(data)
}
getWxList()
function getWxList() {
  dataListLoading.value = true
  getWxListApi(state.queryForm).then((res) => {
    data.value = res.data
    dataListLoading.value = false
  })
}
function setCookie(cvalue) {
  console.log(cvalue)
  document.cookie = 'appid' + '=' + cvalue + ';'
  var x = document.cookie;
  console.log(x)
  ElMessage.warning('设置完成')
  return
}
function selectionChange(selections: any[]) {
  deleteArray.value = []
  state.dataListSelections = selections
  selections.map((item: any) => {
    deleteArray.value.push(item.appid)
  })
}

const deleteBatch = (key) => {
  let data: any[] = key
  if (data.length === 0) {
    ElMessage.warning('请选择删除记录')
    return
  }
  ElMessageBox.confirm('确定进行删除操作?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      deleteApi(data).then(() => {
        ElMessage.success('删除成功')
        getWxList()
        return
      })
    })
    .catch(() => {})
}

const {
  getDataList,
  selectionChangeHandle,
  sizeChangeHandle,
  currentChangeHandle,
  deleteBatchHandle,
  downloadHandle,
  reset
} = useCrud(state)
</script>
